/**
 * 文件名称：  GowinSupport.java
 * 文件版权:  China Gowin Technologies Co., Ltd. Copyright 2007-2012,All rights reserved
 * 文件描述:  <描述>
 * 修改作者:  LiXiangDong
 * 修改时间:  2012-10-26
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.sunshade.common.ibatis;

import java.io.Serializable;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * 数据库处理类
 * 
 * @author  LiXiangDong
 * @version V1.0, 2012-10-26
 */
public class ShadePROSupport implements Serializable
{
    
    /**
     * 注释内容
     */
    private static final long serialVersionUID = 3562846691657057879L;
    
    private static final Logger log = Logger.getLogger(ShadePROSupport.class);
    
    /**
     * 数据源
     */
    private DataSource dataSource = null;
    
    /**
     * 数据源链接
     */
    private Connection connection = null;
    
    /**
     * 存储过程
     */
    private CallableStatement callableStatement = null;
    
    /**
     * 结果集
     */
    private ResultSet resultSet = null;
    
    /**
     * Spring JDBC连接
     */
    private JdbcTemplate template = null; 
    
    /**
     * 设置存储过程的名称
     * 
     * @param procedureName 存储过程名称
     * @throws SQLException 数据库异常
     */
    public void setProcedure(String procedureName)
        throws SQLException
    {
        connection = dataSource.getConnection();
        callableStatement = connection.prepareCall("{ call " + procedureName + "}");
    }
    
    /**
     * 设置输出参数
     *
     * @param parameName 参数名称
     * @param dataType   参数类型
     * @throws SQLException 数据库异常
     */
    public void addParame(String parameName, int dataType)
        throws SQLException
    {
        callableStatement.registerOutParameter(parameName, dataType);
    }
    
    /**
     * 设置输入参数
     *
     * @param parameName 参数名称
     * @param dataType   参数类型
     * @param dataValue  参数值
     * @throws SQLException 数据库异常
     */
    public void addParame(String parameName, int dataType, Object dataValue)
        throws SQLException
    {
        addInputParame(parameName, dataType, dataValue);
    }
    
    /**
     * 设置输入参数类型
     *
     * @param parameterName
     * @param dataType
     * @param dataValue
     * @throws SQLException
     */
    private void addInputParame(String parameterName, int dataType, Object dataValue)
        throws SQLException
    {
        StringBuffer dataValues = new StringBuffer();
        switch (dataType)
        {
            case DataType.CURSOR:
                callableStatement.setObject(parameterName, dataValue);
                break;
            
            case DataType.DOUBLE:
                dataValues.append(dataValue);
                callableStatement.setDouble(parameterName, Double.parseDouble(dataValues.toString()));
                break;
            
            case DataType.INTEGER:
                dataValues.append(dataValue);
                callableStatement.setInt(parameterName, Integer.parseInt(dataValues.toString()));
                break;
            
            case DataType.NUMBER:
                dataValues.append(dataValue);
                callableStatement.setInt(parameterName, Integer.parseInt(dataValues.toString()));
                break;
            
            default:
                callableStatement.setString(parameterName, dataValue.toString());
                break;
        }
    }
    
    /**
     * 关闭数据库连接
     *
     */
    public void close()
    {
        try
        {
            if (null != resultSet)
            {
                resultSet.close();
            }
            if (null != callableStatement)
            {
                callableStatement.close();
            }
            if (null != connection)
            {
                connection.close();
            }
        }
        catch (Exception e)
        {
            log.error("数据库关闭失败", e);
        }
    }
    
    /**
     * 关闭数据库连接
     *
     * @param rs 数据结果集
     */
    public void close(ResultSet rs)
    {
        try
        {
            if (null != resultSet)
            {
                resultSet.close();
            }
            if (null != callableStatement)
            {
                callableStatement.close();
            }
            if (null != connection)
            {
                connection.close();
            }
        }
        catch (Exception e)
        {
            log.error("数据库关闭失败", e);
        }
    }
    
    /**
     * 执行存储过程
     *
     * @throws SQLException
     */
    public void execute()
        throws SQLException
    {
        callableStatement.executeQuery();
    }
    
    /**
     * getInt
     * 
     * @param parameterName
     * @return
     * @throws SQLException
     */
    public int getInt(String parameterName)
        throws SQLException
    {
        return callableStatement.getInt(parameterName);
    }
    
    /**
     * getString
     * 
     * @param parameterName
     * @return
     * @throws SQLException
     */
    public String getString(String parameterName)
        throws SQLException
    {
        return callableStatement.getString(parameterName);
    }
    
    /**
     * getDouble
     * 
     * @param parameterName
     * @return
     * @throws SQLException
     */
    public double getDouble(String parameterName)
        throws SQLException
    {
        return callableStatement.getDouble(parameterName);
    }
    
    /**
     * getDataSet
     * 
     * @param parameterName
     * @return
     * @throws SQLException
     */
    public ResultSet getDataSet(String parameterName)
        throws SQLException
    {
        resultSet = (ResultSet)callableStatement.getObject(parameterName);
        return resultSet;
    }
    
    public DataSource getDataSource()
    {
        return dataSource;
    }
    
    public void setDataSource(DataSource dataSource)
    {
        this.dataSource = dataSource;
    }

    public JdbcTemplate getTemplate()
    {
        return template;
    }

    public void setTemplate(JdbcTemplate template)
    {
        this.template = template;
    }
}